package ftodo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;

import services.FTodoCallback;
import services.FTodoExpandableCallback;
import services.FTodoService;
import util.DateTimeHelper;
import util.ExpandAnimation;
import adapter.FTodoAdapter;
import adapter.FTodoExpandableAdapter;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import background.FTodoAsyn;
import background.FTodoExpandableAsyn;

import com.ftodo.R;
import com.ntnhu2008.actionbar.ActionBar;
import com.ntnhu2008.actionbar.Callback;

import database.FTodoEntities;
import entity.DummyFTodo;
import entity.DummyFTodoExpandable;
import entity.FTodo;

public class FTodoActivity extends ExpandableListActivity {
	public static final int REQUEST_CODE = 1;
	public static boolean isLoad = false;
	private ActionBar actionbar;
	private ListView listView;
	private SeekBar sbTime;
	private LinearLayout layoutProgress;
	private ExpandableListView expand;
	private TextView txtNoTask;
	private EditText txtTextSearch;
	private Button btnDeleteSeach;
	private LinearLayout searchBar;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		getViews();
		setListener();
		Calendar calendar = Calendar.getInstance();
		//calendar.add(Calendar.DATE, 3);
		int date = DateTimeHelper.formatDateAsInt(calendar);
		int time = DateTimeHelper.formatTimeAsInt(calendar);
		FTodo f = new FTodo();
		f.taskname = "Hehe.";
		f.isFinish = 0;
		f.priority = FTodo.PRIORITY_HIGHT;
		f.timeCreate = time;
		f.dateCreate = date;
		FTodoEntities.getInstance(getBaseContext()).insertFTodo(f);

		sbTime.setProgress(0);
	}

	private void getViews() {
		actionbar = (ActionBar) findViewById(R.id.act);
		actionbar.setTitle("FTodo");
		actionbar.addAction(actionSearch);
		actionbar.addAction(actionAdd);
		listView = (ListView) findViewById(R.id.listView);
		sbTime = (SeekBar) findViewById(R.id.sbTime);
		layoutProgress = (LinearLayout) findViewById(R.id.layoutProcess);
		expand = this.getExpandableListView();
		txtNoTask = (TextView) findViewById(R.id.txtNoTask);
		txtTextSearch = (EditText) findViewById(R.id.txtSearchText);
		btnDeleteSeach = (Button) findViewById(R.id.btnSearchDelete);
		searchBar = (LinearLayout) findViewById(R.id.searchBar);
	}

	private void setListener() {
		sbTime.setOnSeekBarChangeListener(onSeekBarChangeListener);
		txtTextSearch.addTextChangedListener(textWatcher);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.ftodo_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuSetting:
			break;
		case R.id.menuClearTaskComplete:
			break;
		case R.id.menuAbout:
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}

	private com.ntnhu2008.actionbar.Action actionSearch = new com.ntnhu2008.actionbar.CallbackAction(
			R.drawable.actionbar_search, new Callback() {
				@Override
				public void onFinish() {
					ExpandAnimation expandAni = new ExpandAnimation(searchBar,
							300);
					searchBar.startAnimation(expandAni);
				}
			});

	private com.ntnhu2008.actionbar.Action actionAdd = new com.ntnhu2008.actionbar.CallbackAction(
			R.drawable.actionbar_add, new Callback() {

				@Override
				public void onFinish() {
					Intent intent = new Intent(FTodoActivity.this,
							AddActivity.class);
					startActivityForResult(intent, REQUEST_CODE);
				}
			});

	private FTodoCallback listTaskByNameCallback = new FTodoCallback() {

		@Override
		public void onFinish(final ArrayList<FTodo> ftodos, boolean finish) {
			isLoad = finish;
			layoutProgress.setVisibility(View.GONE);
			expand.setVisibility(View.GONE);
			listView.setVisibility(View.GONE);
			txtNoTask.setVisibility(View.GONE);
			if (ftodos.size() > 0) {
				listView.setVisibility(View.VISIBLE);
				FTodoAdapter adapter = new FTodoAdapter(FTodoActivity.this,
						ftodos);
				listView.setAdapter(adapter);
			} else {
				txtNoTask.setVisibility(View.VISIBLE);
			}
		}
	};

	private FTodoCallback simpleCallback = new FTodoCallback() {

		@Override
		public void onFinish(ArrayList<FTodo> ftodos, boolean finish) {
			isLoad = finish;
			layoutProgress.setVisibility(View.GONE);
			expand.setVisibility(View.GONE);
			listView.setVisibility(View.GONE);
			txtNoTask.setVisibility(View.GONE);
			if (ftodos.size() > 0) {
				listView.setVisibility(View.VISIBLE);
				FTodoAdapter adapter = new FTodoAdapter(FTodoActivity.this,
						ftodos);
				listView.setAdapter(adapter);
			} else {
				txtNoTask.setVisibility(View.VISIBLE);
			}
		}
	};

	private FTodoExpandableCallback multiCallback = new FTodoExpandableCallback() {

		@Override
		public void onFinish(final ArrayList<Integer> groupDates,
				final ArrayList<ArrayList<FTodo>> ftodos, boolean finish) {
			isLoad = finish;
			layoutProgress.setVisibility(View.GONE);
			expand.setVisibility(View.GONE);
			listView.setVisibility(View.GONE);
			txtNoTask.setVisibility(View.GONE);
			if (groupDates.size() > 0) {
				expand.setVisibility(View.VISIBLE);
				Collections.sort(groupDates);
				ArrayList<String> groups = new ArrayList<String>();
				for (int i : groupDates) {
					groups.add(DateTimeHelper.getFormattedDate(i));
				}
				setListAdapter(new FTodoExpandableAdapter(FTodoActivity.this,
						groups, ftodos));
			} else {
				txtNoTask.setVisibility(View.VISIBLE);
			}
		}

	};

	private TextWatcher textWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(final CharSequence s, int start, int before,
				int count) {
			if (isLoad || s.equals("")) {
				return;
			}
			layoutProgress.setVisibility(View.VISIBLE);
			DummyFTodo dummyFTodo = new DummyFTodo(getBaseContext(),
					listTaskByNameCallback, -1, s.toString(), null);
			new FTodoAsyn().execute(dummyFTodo);
			isLoad = true;
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		@Override
		public void afterTextChanged(Editable s) {

		}
	};

	private OnSeekBarChangeListener onSeekBarChangeListener = new OnSeekBarChangeListener() {

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {

		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {

		}

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {

			if (isLoad) {
				return;
			}
			layoutProgress.setVisibility(View.VISIBLE);
			Calendar calendarFrom = Calendar.getInstance();

			Calendar calendarTo = Calendar.getInstance();
			calendarTo.add(Calendar.DATE, progress);

			if (progress == 0) {
				int date = DateTimeHelper.formatDateAsInt(calendarFrom);
				DummyFTodo dummyFTodo = new DummyFTodo(getBaseContext(),
						simpleCallback, date, null, null);
				new FTodoAsyn().execute(dummyFTodo);
				isLoad = true;
			} else {
				int dateFrom = DateTimeHelper.formatDateAsInt(calendarFrom);
				int dateTo = DateTimeHelper.formatDateAsInt(calendarTo);
				DummyFTodoExpandable dummyFTodoExpandable = new DummyFTodoExpandable(
						getBaseContext(), dateFrom, dateTo, multiCallback,
						null, null);
				new FTodoExpandableAsyn().execute(dummyFTodoExpandable);
				isLoad = true;
			}
		}
	};
}